home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 20 / Cream of the Crop 20 (Terry Blount) (1996).iso / os2 / sysb091a.zip / sysbench / src / pmb_hanoi.c < prev    next >
C/C++ Source or Header  |  1996-05-18  |  10KB  |  465 lines

  1. /************************************************/
  2. /*  hanoi.c, Version 1.0, 18 Sep 1992  --- AAA  */
  3. /*           Version 1.1, 06 Jul 1993  --- AAA  */
  4. /*           Added print out for all disks      */
  5. /*           Version 1.2, 28 May 1994  --- AAA  */
  6. /*           Calculate average Moves/25usec     */
  7. /************************************************/
  8.  
  9. /**************************************************/
  10. /* Adapted from the MUSBUS test suite.            */
  11. /* hanoi.c, V3.5 87/08/06 08:11:14 kenj           */
  12. /*                                                */
  13. /* Various timer routines are included. Please    */
  14. /* select the appropriate timer routine for your  */
  15. /* system from the options below. If no option is */
  16. /* available then you will need to create your    */
  17. /* own similar 'hanoi_time()' timer routine.           */
  18. /*                                                */
  19. /* You can uncomment one of the timer options     */
  20. /* below to use it, or you can compile with the   */
  21. /* option name. For example you can compile with  */
  22. /* '-DUNIX', or '-DVMS', '-DMSC',..., etc.        */
  23. /*                                                */
  24. /* Example compilation:                           */
  25. /*  cc -DUNIX hanoi.c -o hanoi, or                */
  26. /* gcc -DUNIX -O hanoi.c -o hanoi, or             */
  27. /*  cc -dAmiga hanoi.c -o hanoi, ..., etc         */
  28. /**************************************************/
  29.  
  30. #define INCL_DOSMISC
  31. #include <stdio.h>
  32. #include <time.h>
  33. #include <os2.h>
  34.  
  35. /***************************************************************/
  36. /* Timer options. You MUST uncomment one of the options below  */
  37. /* or compile, for example, with the '-DUNIX' option.          */
  38. /***************************************************************/
  39. /* #define Amiga       */
  40. /* #define UNIX        */
  41. /* #define UNIX_Old    */
  42. /* #define VMS         */
  43. /* #define BORLAND_C   */
  44. /* #define MSC         */
  45. /* #define MAC         */
  46. /* #define IPSC        */
  47. /* #define FORTRAN_SEC */
  48. /* #define GTODay      */
  49. /* #define CTimer      */
  50. /* #define UXPM        */
  51.  
  52. #define other(i,j) (6-(i+j))
  53.  
  54. static double TimeArray[3];
  55. static int num[4];
  56. static long count;
  57.  
  58. static mov(int n, int f,int t);
  59. static hanoi_time(double *p);
  60.  
  61.  
  62. double pmb_hanoi(void)
  63. {
  64.   double RunTime = 0.0, sum_mps = 0.0, TLimit, mps;
  65.   int disk, Loops = 0;
  66.  
  67. //printf("\n");
  68. //printf("Towers of Hanoi Puzzle Test Program\n");
  69. //printf("Disks     Moves     Time(sec)   Moves/25usec\n");
  70.  
  71. TLimit  = (30.0);
  72. disk    = 15;
  73.  
  74. while ( RunTime < TLimit )
  75. {
  76.  disk++;
  77.  num[0] = 0;
  78.  num[1] = disk;
  79.  num[2] = 0;
  80.  num[3] = 0;
  81.  count  = 0;
  82.  
  83.  hanoi_time(TimeArray);
  84.  mov(disk,1,3);
  85.  hanoi_time(TimeArray);
  86.  
  87.  RunTime = TimeArray[1];
  88.  if (RunTime != 0.0)
  89.     {
  90.      mps = 2.5E-05 * ( (double)count/RunTime );
  91.      Loops = Loops + 1;
  92.      sum_mps = sum_mps + mps;
  93.      //printf("%3ld  %10ld  %12.5lf  %10.4lf\n",disk,count,RunTime,mps);
  94.      }
  95.  if ( disk == 30 ) break;
  96. }
  97.  
  98. sum_mps = sum_mps / (double)Loops;
  99. //printf("\nAverage Moves Per 25 usec = %10.4lf\n",sum_mps);
  100. //printf("\n");
  101.  
  102. return sum_mps;
  103. }
  104.  
  105. static mov(n,f,t)
  106. {
  107.    int o;
  108.    if(n == 1)
  109.    {
  110.       num[f]--;
  111.       num[t]++;
  112.       count++;
  113.       return(0);
  114.    }
  115.    o = other(f,t);
  116.    mov(n-1,f,o);
  117.    mov(1,f,t);
  118.    mov(n-1,o,t);
  119.    return(0);
  120. }
  121.  
  122. /*****************************************************/
  123. /* Various timer routines.                           */
  124. /* Al Aburto, aburto@marlin.nosc.mil, 26 Sep 1992    */
  125. /*                                                   */
  126. /* hanoi_time(p) outputs the elapsed time seconds in p[1] */
  127. /* from a call of hanoi_time(p) to the next call of       */
  128. /* hanoi_time(p).  Use CAUTION as some of these routines  */
  129. /* will mess up when timing across the hour mark!!!  */
  130. /*                                                   */
  131. /* For timing I use the 'user' time whenever         */
  132. /* possible. Using 'user+sys' time is a separate     */
  133. /* issue.                                            */
  134. /*                                                   */
  135. /*****************************************************/
  136.  
  137. /*************************************/
  138. /* Timer code.                       */
  139. /*************************************/
  140. /*******************/
  141. /*  Amiga hanoi_time()  */
  142. /*******************/
  143. #ifdef Amiga
  144. #include <ctype.h>
  145. #define HZ 50
  146.  
  147. hanoi_time(p)
  148. double p[];
  149. {
  150.    double q;
  151.  
  152.    struct   tt {
  153.       long  days;
  154.       long  minutes;
  155.       long  ticks;
  156.    } tt;
  157.  
  158.    q = p[2];
  159.  
  160.    DateStamp(&tt);
  161.  
  162.    p[2] = ( (double)(tt.ticks + (tt.minutes * 60L * 50L)) ) / (double)HZ;
  163.    p[1] = p[2] - q;
  164.  
  165.    return 0;
  166. }
  167. #endif
  168.  
  169. /*****************************************************/
  170. /*  UNIX hanoi_time(). This is the preferred UNIX timer.  */
  171. /*  Provided by: Markku Kolkka, mk59200@cc.tut.fi    */
  172. /*  HP-UX Addition by: Bo Thide', bt@irfu.se         */
  173. /*****************************************************/
  174. #ifdef UNIX
  175. #include <sys/time.h>
  176. #include <sys/resource.h>
  177.  
  178. #ifdef hpux
  179. #include <sys/syscall.h>
  180. #define getrusage(a,b) syscall(SYS_getrusage,a,b)
  181. #endif
  182.  
  183. struct rusage rusage;
  184.  
  185. hanoi_time(p)
  186. double p[];
  187. {
  188.    double q;
  189.  
  190.    q = p[2];
  191.  
  192.    getrusage(RUSAGE_SELF,&rusage);
  193.  
  194.    p[2] = (double)(rusage.ru_utime.tv_sec);
  195.    p[2] = p[2] + (double)(rusage.ru_utime.tv_usec) * 1.0e-06;
  196.    p[1] = p[2] - q;
  197.  
  198.    return 0;
  199. }
  200. #endif
  201.  
  202. /***************************************************/
  203. /*  UNIX_Old hanoi_time(). This is the old UNIX timer.  */
  204. /*  Use only if absolutely necessary as HZ may be  */
  205. /*  ill defined on your system.                    */
  206. /***************************************************/
  207. #ifdef UNIX_Old
  208. #include <sys/types.h>
  209. #include <sys/times.h>
  210. #include <sys/param.h>
  211.  
  212. #ifndef HZ
  213. #define HZ 60
  214. #endif
  215.  
  216. struct tms tms;
  217.  
  218. hanoi_time(p)
  219. double p[];
  220. {
  221.    double q;
  222.  
  223.    q = p[2];
  224.  
  225.    times(&tms);
  226.  
  227.    p[2] = (double)(tms.tms_utime) / (double)HZ;
  228.    p[1] = p[2] - q;
  229.  
  230.    return 0;
  231. }
  232. #endif
  233.  
  234. /*********************************************************/
  235. /*  VMS hanoi_time() for VMS systems.                         */
  236. /*  Provided by: RAMO@uvphys.phys.UVic.CA                */
  237. /*  Some people have run into problems with this timer.  */
  238. /*********************************************************/
  239. #ifdef VMS
  240. #include time
  241.  
  242. #ifndef HZ
  243. #define HZ 100
  244. #endif
  245.  
  246. struct tbuffer_t
  247.        {
  248.     int proc_user_time;
  249.     int proc_system_time;
  250.     int child_user_time;
  251.     int child_system_time;
  252.        };
  253. struct tbuffer_t tms;
  254.  
  255. hanoi_time(p)
  256. double p[];
  257. {
  258.    double q;
  259.  
  260.    q = p[2];
  261.  
  262.    times(&tms);
  263.  
  264.    p[2] = (double)(tms.proc_user_time) / (double)HZ;
  265.    p[1] = p[2] - q;
  266.  
  267.    return 0;
  268. }
  269. #endif
  270.  
  271. /******************************/
  272. /*  BORLAND C hanoi_time() for DOS */
  273. /******************************/
  274. #ifdef BORLAND_C
  275. #include <ctype.h>
  276. #include <dos.h>
  277. #include <time.h>
  278.  
  279. #define HZ 100
  280. struct time tnow;
  281.  
  282. hanoi_time(p)
  283. double p[];
  284. {
  285.    double q;
  286.  
  287.    q = p[2];
  288.  
  289.    gettime(&tnow);
  290.  
  291.    p[2] = 60.0 * (double)(tnow.ti_min);
  292.    p[2] = p[2] + (double)(tnow.ti_sec);
  293.    p[2] = p[2] + (double)(tnow.ti_hund)/(double)HZ;
  294.    p[1] = p[2] - q;
  295.  
  296.    return 0;
  297. }
  298. #endif
  299.  
  300. /**************************************/
  301. /*  Microsoft C (MSC) hanoi_time() for DOS */
  302. /**************************************/
  303. #ifdef MSC
  304. #include <time.h>
  305. #include <ctype.h>
  306.  
  307. #define HZ CLK_TCK
  308. clock_t tnow;
  309.  
  310. hanoi_time(p)
  311. double p[];
  312. {
  313.    double q;
  314.  
  315.    q = p[2];
  316.  
  317.    tnow = clock();
  318.  
  319.    p[2] = (double)tnow / (double)HZ;
  320.    p[1] = p[2] - q;
  321.  
  322.    return 0;
  323. }
  324. #endif
  325.  
  326. /*************************************/
  327. /*  Macintosh (MAC) Think C hanoi_time()  */
  328. /*************************************/
  329. #ifdef MAC
  330. #include <time.h>
  331.  
  332. #define HZ 60
  333.  
  334. hanoi_time(p)
  335. double p[];
  336. {
  337.    double q;
  338.  
  339.    q = p[2];
  340.  
  341.    p[2] = (double)clock() / (double)HZ;
  342.    p[1] = p[2] - q;
  343.  
  344.    return 0;
  345. }
  346. #endif
  347.  
  348. /************************************************************/
  349. /*  iPSC/860 (IPSC) hanoi_time() for i860.                       */
  350. /*  Provided by: Da